#include "stdafx.h"
#include "FxMap.h"

CFxMap::CFxMap(void)
{
	elementCount = 0;

	for(int i=0; i<MAX_ELEMENT_COUNT; i++)
		map[i].valueObject = NULL;
}

CFxMap::~CFxMap(void)
{
	Clear();
}

BOOL CFxMap::SetString(char* keyname, char* value)
{
	if(elementCount >= MAX_ELEMENT_COUNT)
		return FALSE;

	int index = lookup(keyname);

	if(index == -1) // without
	{
		strcpy(map[elementCount].keyname, keyname);
		strcpy(map[elementCount].valueString, value);
		elementCount++;
	}
	else
		strcpy(map[index].valueString, value);

	return TRUE;
}

char* CFxMap::GetString(char* keyname)
{
	int index = lookup(keyname);

	if(index == -1)
		return NULL;
	
	return map[index].valueString;
}

BOOL CFxMap::SetInt(char* keyname, int value)
{
	if(elementCount >= MAX_ELEMENT_COUNT)
		return FALSE;

	int index = lookup(keyname);

	if(index == -1) // without
	{
		strcpy(map[elementCount].keyname, keyname);
		map[elementCount].valueInt = value;
		elementCount++;
	}
	else
		map[index].valueInt = value;

	return TRUE;
}

int CFxMap::GetInt(char* keyname)
{
	int index = lookup(keyname);

	if(index == -1)
		return -999999;
	
	return map[index].valueInt;
}

BOOL CFxMap::SetDouble(char* keyname, double value)
{
	if(elementCount >= MAX_ELEMENT_COUNT)
		return FALSE;

	int index = lookup(keyname);

	if(index == -1) // without
	{
		strcpy(map[elementCount].keyname, keyname);
		map[elementCount].valueDouble = value;
		elementCount++;
	}
	else
		map[index].valueDouble = value;

	return TRUE;
}

double CFxMap::GetDouble(char* keyname)
{
	int index = lookup(keyname);

	if(index == -1)
		return -999999;
	
	return map[index].valueDouble;
}




BOOL CFxMap::SetString(int keyvalue, char* value)
{
	if(elementCount >= MAX_ELEMENT_COUNT)
		return FALSE;

	int index = lookup(keyvalue);

	if(index == -1) // without
	{
		map[elementCount].keyvalue = keyvalue;
		strcpy(map[elementCount].valueString, value);
		elementCount++;
	}
	else
		strcpy(map[index].valueString, value);

	return TRUE;
}

char* CFxMap::GetString(int keyvalue)
{
	int index = lookup(keyvalue);

	if(index == -1)
		return NULL;
	
	return map[index].valueString;
}

BOOL CFxMap::SetInt(int keyvalue, int value)
{
	if(elementCount >= MAX_ELEMENT_COUNT)
		return FALSE;

	int index = lookup(keyvalue);

	if(index == -1) // without
	{
		map[elementCount].keyvalue = keyvalue;
		map[elementCount].valueInt = value;
		elementCount++;
	}
	else
		map[index].valueInt = value;

	return TRUE;
}

int CFxMap::GetInt(int keyvalue)
{
	int index = lookup(keyvalue);

	if(index == -1)
		return -999999;
	
	return map[index].valueInt;
}

BOOL CFxMap::SetDouble(int keyvalue, double value)
{
	if(elementCount >= MAX_ELEMENT_COUNT)
		return FALSE;

	int index = lookup(keyvalue);

	if(index == -1) // without
	{
		map[elementCount].keyvalue = keyvalue;
		map[elementCount].valueDouble = value;
		elementCount++;
	}
	else
		map[index].valueDouble = value;

	return TRUE;
}

double CFxMap::GetDouble(int keyvalue)
{
	int index = lookup(keyvalue);

	if(index == -1)
		return -999999;
	
	return map[index].valueDouble;
}


BOOL CFxMap::SetObject(char* keyname, void* object)
{
	if(elementCount >= MAX_ELEMENT_COUNT)
		return FALSE;

	int index = lookup(keyname);

	if(index == -1) // without
	{
		strcpy(map[elementCount].keyname, keyname);
		map[elementCount].valueObject = object;
		elementCount++;
	}
	else
		map[index].valueObject = object;

	return TRUE;
}

void* CFxMap::GetObject(char* keyname)
{
	int index = lookup(keyname);

	if(index == -1)
		return NULL;
	
	return map[index].valueObject;
}

BOOL CFxMap::SetObject(int keyvalue, void* object)
{
	if(elementCount >= MAX_ELEMENT_COUNT)
		return FALSE;

	int index = lookup(keyvalue);

	if(index == -1) // without
	{
		map[elementCount].keyvalue = keyvalue;
		map[elementCount].valueObject = object;
		elementCount++;
	}
	else
		map[index].valueObject = object;

	return TRUE;
}

void* CFxMap::GetObject(int keyvalue)
{
	int index = lookup(keyvalue);

	if(index == -1)
		return NULL;
	
	return map[index].valueObject;
}


int CFxMap::lookup(char* keyname)
{
	for(int i=0; i<elementCount; i++)
	{
		if(!strcmp(map[i].keyname, keyname)) // matching
			return i;
	}

	return -1;
}

int CFxMap::lookup(int keyvalue)
{
	for(int i=0; i<elementCount; i++)
	{
		if(map[i].keyvalue == keyvalue) // matching
			return i;
	}

	return -1;
}

void CFxMap::Clear()
{
	for(int i=0; i<MAX_ELEMENT_COUNT; i++)
		if(map[i].valueObject != NULL)
			delete map[i].valueObject;

	elementCount = 0;
}